<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <title>登录</title>
    <link rel="stylesheet" href="../../css/LXW/login.css" type="text/css" />
    <link rel="stylesheet" href="../../js/LXW/shipei.js">
</head>

<body>
    <div class="top-bar">
        <!-- <div class="login-text">
            <h3>密码登录</h3>
        </div> -->

        <div class="input">
            <div class="username">
                <span>用户名 :</span>
                <input id="username" type="text" placeholder="请输入用户名">
            </div>
            <div class="password">
                <span>密&nbsp;&nbsp;&nbsp;&nbsp;码 :</span>
                <input id="password" type="password" placeholder="请输入密码">
            </div>
        </div>

        <div class=login>
            <button id="login"><a href="#">登录</a></button>
            <button id="register"><a href="./register.html">注册</a></button>
        </div>
        <div></div>
    </div>
</body>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script>
    let usernameEl = document.querySelector('#username');
    let passwordEl = document.querySelector('#password');
    let loginBtn = document.querySelector('#login');
    let registerBtn = document.querySelector('#register');

    function tizhuan(token, username) {

        localStorage.setItem("token", token)
        localStorage.setItem("username", username)
        window.history.back();
        location.href = '../JLS/home.html'
    }

    loginBtn.addEventListener("click", async function () {
        let username = usernameEl.value;
        let password = passwordEl.value;

        let result = await axios({
            method: 'post',
            url: 'http://127.0.0.1:9527/api/users/login',
            data: {
                username,
                password,
            },
        }).then(res => {
            // data -> 服务器返回的所有数据
            let data = res.data;
            if (data.code !== 0) {
                return alert(data.msg)
            } else {
                let token = data.token;
                localStorage.setItem("token", token)
                localStorage.setItem("username", username)
                 
                tizhuan (token, username)
                // console.log(token);

                // // localStorage.token = token;
                // // alert(data.msg)

                // // let timer = setTimeout(() => {

                //     // location.href = './message.html'

                // // })

            }
        })

    })
</script>

</html>